package com.SmartCare.service.impl.moments;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.Result;
import com.SmartCare.mapper.momments.LikeInfoMapper;
import com.SmartCare.pojo.moments.LikeInfo;
import com.SmartCare.service.moments.LikeInfoService;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class LikeInfoServiceImpl extends ServiceImpl<LikeInfoMapper, LikeInfo> implements LikeInfoService {
    //点赞动态
    @Override
    public Result likeDynamic(Long userId, Long dynamicId) {
            LikeInfo info = new LikeInfo();
            info.setUserId(userId);
            info.setDynamicId(dynamicId);
            boolean res = save(info);
            if (res){
                LambdaUpdateWrapper<LikeInfo> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(LikeInfo::getUserId,userId);
                wrapper.eq(LikeInfo::getDynamicId,dynamicId);
                LikeInfo one = getOne(wrapper);

                Map<String ,Long> map=new HashMap<>();
                map.put("id",one.getId());
                return Result.success("点赞成功",map);
            }
        return Result.error("动态已点赞");
    }

    //取消点赞
    @Override
    public Result cancelLikeDynamic(Long id) {

        LambdaUpdateWrapper<LikeInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LikeInfo::getId,id);
        boolean res = remove(wrapper);
        if (res){
            return Result.success("取消点赞成功");
        }
        return Result.error("取消点赞失败");
    }

}
